package com.yyx.demo.queue.test;

import com.yyx.demo.queue.ZKDistributedQueue;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class QueueTest2 implements Watcher {

    public static final String zkServerPath = "10.2.132.171:2181";
    public static final Integer timeout = 50000;

    private ZooKeeper createClient() throws IOException {
        ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new QueueTest2());
        return zk;
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        QueueTest2 test=new QueueTest2();
        ZooKeeper zooKeeper=test.createClient();
        ZKDistributedQueue zkDistributedQueue=new ZKDistributedQueue(zooKeeper,"/topic",null);
        String a=null;
//        for (int i = 0; i < 5 ; i++) {
//            a=a+"t";
//            System.out.println("\n\n\noffer::"+a);
//            zkDistributedQueue.offerSync(a.getBytes());
//        }
        for (int i = 0; i < 5 ; i++) {
            System.out.println("\n\n\ntake::"+new String(zkDistributedQueue.takeSync()));
        }
    }

    @Override
    public void process(WatchedEvent event) {

    }
}
